<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ****************************************************************************
  NAIYUMIE KING WANG JJANG Front End Developer <naiyumie@gmail.com>
  FileType:CRLF, Encording:UTF-8, Tab&Space:4/4
  ------------------------------------------------------------------------
  샘플 개발
**************************************************************************** */

class DevCrudSample extends N_Controller {

	/***************************************************
	 * preprocess
	 *     constructor
	 *     function
	 *     declaration
	 ***************************************************/
	public function __construct(){
		parent::__construct();

		$this->nUriExtend = array(
			'listSeg' => 'layoutSample',
			'txSeg' => 'testProcessAjax',
			'ntxSeg' => 'testListAjax',
			'createSeg' => 'layoutSample'
		);
		$this->nUri = array_merge($this->nUri, $this->nUriExtend);

		$this->load->model('ADeveloperSampleM');
	}

	public function index() {
		$this->layoutSample();
	}


	/***************************************************
	 * view control : Layouts
	 ***************************************************/
	public function layoutSample(){
		# local variables
		$retVal = '';
		$mode = '';

		$this->tilesHeader();

		$mode = empty(trim($this->nurl->decrypt($this->input->get('mode'))))? 'list' : trim($this->nurl->decrypt($this->input->get('mode')));
		//echo $mode;
		switch($mode){
			case 'retrieve' :
				$pk_uid = trim($this->nurl->decrypt($this->input->get('article')));
				$r01 = $this->ADeveloperSampleM->r01(array('pk_uid'=>$pk_uid));
				$viewArray = array('mode'=>$mode, 'r'=>$r01, 'subjection'=>'상세 - 조회 / 수정');
			break;
			case 'create' :
				$viewArray = array('mode'=>$mode, 'subjection'=>'상세 - 새 레코드 추가');
			break;
			case 'list' :
				//$m01 = $this->ADeveloperSampleM->m01();
				$viewArray = array('mode'=>$mode);
			break;
			default:break;
		}
		$this->load->view('vDevCrudSample', $viewArray);
		$this->tilesFooter();
	}


	/***************************************************
	 * ajax process : List
	 ***************************************************/
	public function testListAjax(){
		# local variables
		$retVal = '';
		$modelworks = array();
		$data = array();
		$r01 = array();
		$modelSetVariables = array();

		# pagenation
		if(empty($isWhere) === FALSE){
			$modelSetVariables['where'] = $isWhere;
			//nlog('where+++');
		}
		//	nlog('where+++' . print_r($modelSetVariables, true));

		//$totalCount = $this->MembersM->r02(          $test  );
		$totalCount = $this->ADeveloperSampleM->r02();
		//nlog('tc'.$totalCount);


		$modelSetVariables['paginationCfg'] = array(
			'seg1Arg' => $this->nUri['seg1'],
			'seg2Arg' => $this->nUri['seg2'],
			'articles_count' => 10,
			'page_size'	=> 10,
			'totalCount' => $totalCount,
			'get' => 'p1'
		);
		$this->npagination->init($modelSetVariables['paginationCfg']);
		$modelSetVariables['paginationCfg']['paginationFirst'] = $this->npagination->paginationCfg['paginationFirst'];
		$modelSetVariables['paginationCfg']['paginationSecond'] = $this->npagination->paginationCfg['paginationSecond'];

		$r01 = $this->ADeveloperSampleM->r01($modelSetVariables);
		//nlog('tc'.print_r($r01, true));

		$pagination = $this->npagination->out();
		//nlog('r01'.print_r($r01, true));
		//nlog('pagination'.$pagination);
		//nlog('totalCount'.$totalCount);

		# list data
		$dataln = '';
		foreach ($r01 as $key => $val) {
			$dataln .= sprintf('
				<tr onclick="%s">
					<td>%s</td>
					<td>%s</td>
					<td>%s</td>
					<td>%s</td>
					<td>%s</td>
					<td>%s</td>
					<td>%s</td>
					<td>%s</td>
					<td>%s</td>
					<td>%s</td>
					<td>%s</td>
				</tr>',
				$this->nmover->jsLink(array('seg1'=>$this->nUri['seg1'], 'seg2'=>$this->nUri['listSeg'], 'queryString'=>array( 'mode'=>'retrieve', 'article'=>$val['pk_uid'] ))),
				$val['pk_uid'],
				$val['st_text_field01'],
				$val['st_text_field02'],
				$val['no_text_field03'],
				$val['dt_date_field01'],
				$val['tm_time_field01'],
				$val['pw_password_field01'],
				$val['fg_checkbox_field01'],
				$val['fg_radio_field01'],
				$val['fg_select_field01'],
				$val['fg_select_field02']
			);
		}

		# out put json
		$data['listdata'] = $dataln;
		$data['pagination'] = $pagination;
		$retVal = json_encode($data);
		exit($retVal);
	}


	/***************************************************
	 * ajax process : Cud
	 ***************************************************/
	public function testProcessAjax(){
		# local variables
		$retVal = '';
		$mode = '';
		$modelworks = array();
		$data = array();
		$process = '';

		# decoding and set mode
		//$data['tmp___posts'] = $this->input->post();
		$this->validatedValues = $this->nvalidation->postAesDecodes();
		$mode = trim($this->nurl->decrypt($this->input->get('mode')));
		// nPrt($mode);

		# for debug
		$data['tmp___validatedValues'] = print_r($this->validatedValues, TRUE);
		$data['tmp___validatedValues2'] = json_encode($this->validatedValues);

		# Validation Phase
		if($mode == 'create' || $mode == 'update'){
			if(!$this->nvalidation->basicValidation(array('mode'=>$mode,'validationValueArray'=>$this->validatedValues,'fg_tableName'=>'tbl_a_developer_sample'))){
				$data['mode'] = $mode;
				$data['process'] = 'validation';
				$data['resultMessage'] = 'fail';
				$data['vStatArray'] = json_encode($this->nvalidation->vStatFailedResultArray);
				$retVal = json_encode($data);
				exit($retVal);
			}
		}

		#delete validation phase
		if($mode == 'delete'){
			//nlog(print_r($this->validatedValues, true));
			foreach($this->validatedValues as $key=>$val){
				if($key != 'pk_uid'){
					unset($this->validatedValues[$key]);
				}
			}
			if(!$this->nvalidation->basicValidation(array('mode'=>$mode,'validationValueArray'=>$this->validatedValues))){
				//print('>>> basicValidation valError');
				$data['mode'] = $mode;
				$data['process'] = 'validation';
				$data['processPhase'] = 'basic';
				$data['resultMessage'] = 'fail';
				$retVal = json_encode($data);
				exit($retVal);
			}
			//nlog(print_r($this->validatedValues, true));
		}

		# for debug
		$data['tmp___this->nvalidation->vStatFailedResultArray'] = count($this->nvalidation->vStatFailedResultArray);

		# db Transaction Phase
		if(@$this->validatedValues['pk_uid'] == '' && $mode == 'create'){
			$process .= 'create';
			$modelworks = $this->ADeveloperSampleM->c01($this->validatedValues);
		} else if($mode == 'update' || $mode == 'retrieve') {
			$process .= 'update';
			$modelworks = $this->ADeveloperSampleM->u01($this->validatedValues);
		} else if(@$this->validatedValues['pk_uid'] != '' && $mode == 'delete'){
			$process .= 'delete';
			$modelworks = $this->ADeveloperSampleM->d01($this->validatedValues);
		}

		# out put json
		$data['mode'] = $mode;
		$data['process'] = $process;
		$data['resultMessage'] = $modelworks ? 'succ' : 'fail';
		$retVal = json_encode($data);
		exit($retVal);
	}

}
/* End of file */